<!-- HONG SHIN   -->

<html>
	<head><title>My little query page</title></head>
	
	<body>
	<h1>My little query page</h1>
	<h2>Type a SQL Query in the following box:</h2>

	<form method="GET" action="./query.php">
	<textarea name="query" rows=15 cols=60></textarea>
	<input type="submit" value="Submit Query">
	</form>

	<?php
		$input = $_GET["query"];
		if($input) 
		{
			$db_connection = mysql_connect("localhost", "cs143", "");
			mysql_select_db("CS143", $db_connection);
			$rs = mysql_query($input, $db_connection);
			/* Check for errors (malformed expression)  */
			if (! $rs )
			{
				die('Invalid query: ' . mysql_error());
			}

			$num_cols = mysql_num_fields($rs);
			$num_rows = mysql_num_rows($rs);


			echo "<h3> MySQL query result: </h3>";

			echo "<table border=\"1\" align=\"left\">";
			echo "<tr>";
			for ($j = 0; $j < $num_cols; $j++)
			{
				echo "<th>" . mysql_field_name($rs, $j) . "</th>";
			}
			echo "</tr>";

		//	while($row = mysql_fetch_row($rs))
		//	for ($i = 0; $i < $num_rows; $i++)
			while($row = mysql_fetch_array($rs))
			{
				echo "<tr>";
				for ($k = 0; $k < $num_cols; $k++)
				{
					echo "<td>" . $row[mysql_field_name($rs, $k)] . 
					"</td>";	
				}
				echo "</tr>";
				
			}
			echo "</table>";
			mysql_close($db_connection);
		}

	?>

	</body>


</html>









